<template>
	<el-form :model="formData" ref="formRef" :rules="formRules" label-width="60px" label-position="left">
		<el-form-item label="编码：" prop="code">
        	<el-input v-model.trim="formData.code" v-if="status === 0" maxLength="5" @keyup.enter.native="enterKey($event, 'MA1')"></el-input>
        	<el-input v-model.trim="formData.code" v-if="status === 1" maxLength="5" readonly></el-input>
        </el-form-item>
        <el-form-item label="名称：" prop="name">
            <el-input v-model.trim="formData.name" ref="MA1" @keyup.enter.native="save('formRef')"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" size="small" @click="save('formRef')">确定</el-button>
            <el-button type="danger" size="small" @click="close">取消</el-button>
        </el-form-item>
	</el-form>
</template>
<script>
export default {
	data() {
		return {
			formData: {},
			formRules: {
				code: [{required: true, message: '编码不能为空', triggle: 'blur'}],
				name: [{required: true, message: '名称不能为空', triggle: 'blur'}],
			},
			status: null,		//是编辑还是新增 0 == 新增 1 == 编辑
		}
	},
	methods: {
		getUpdateData() {		//获取更新数据
			let groupNode = this.$store.state.accountSubjectStore.groupNode;
			this.$http.get('subjectGroup/selectSubjectByObject?id=' + groupNode['id']).then(res => {
				if(res.data.code == 20001) {
					this.formData = res.data.data[0];
				}
			});
		},
		save(formName) {
			this.$refs[formName].validate((valid) => {
                if (valid) {
                	if(this.formData['code'].length != 5) {
                        this.$message({type: 'warning', duration: 1500, message: '编码长度只能为5位数字'});
                    } else if(isNaN(this.formData['code'])) {
                        this.$message({type: 'warning', duration: 1500, message: '编码包含非数字'});
                    } else {
                    	let groupNode = this.$store.state.accountSubjectStore.groupNode;
                    	let data = {
                            code: this.formData['code'],
                            name: this.formData['name'],
                            type: groupNode['id'],    //父级id
                        }
                        if(this.formData['id']) {
                        	//修改
                        	this.$http({
		                        url: 'subjectGroup/insertOrUpdateByGroup',
		                        method: 'post',
		                        params: data
		                    }).then(res => {
		                        if(res.data.code == 20001) {
		                            this.$message.success({message: '编辑成功', duration: 1500, type: 'success'});
		                            setTimeout(() => {
		                            	this.$store.dispatch('accountSubjectStore/getGroupTreeData');
		                                this.$store.dispatch('accountSubjectStore/getTreeData');
	                                    this.close();
		                            }, 1000);
		                        } else {
		                            this.$message.error({message: res.data.msg || '编辑失败', duration: 1000, type: 'error'});
		                        }
		                    });
                        } else {
                        	//新增
                        	this.$http({
	                            url: 'subjectGroup/insertOrUpdateByGroup',
	                            method: 'post',
	                            params: data
	                        }).then(res => {
	                            if(res.data.code == 20001) {
	                                this.$message({type: 'success', duration: 1500, message: '新增成功'});
	                                setTimeout(() => {
	                                	this.$store.dispatch('accountSubjectStore/getGroupTreeData');
	                                    this.$store.dispatch('accountSubjectStore/getTreeData');
	                                    this.close();
	                                }, 1000);
	                            } else {
	                                this.$message.error({message: res.data.msg || '新增失败', duration: 1500, type: 'error'});
	                            }
	                        });
                        }
                    }
                }
            });
		},
		close() {
			let index = parent.layer.getFrameIndex(window.name);    //先得到当前iframe层的索引
            parent.layer.close(index);
		},
		enterKey(lastRef, nextRef) {		//回车键换行
            if(lastRef.target.value) {
                this.$refs[nextRef].focus();
            }
        }
	},
	created() {
		let status = this.$store.state.accountSubjectStore.groupStatus;
		this.status = status[0];
		if(this.status === 1) {
			//编辑
			this.getUpdateData();
		}
	}
}
</script>